<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-model-item label="考试" prop="examinationId">
              <j-dict-select-tag v-model="queryParam.examinationId" placeholder="请选择所属考试" dictCode="ach_examination,examination_name,id"/>
            </a-form-model-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-model-item label="科目" prop="subjectId">
              <j-dict-select-tag v-model="queryParam.subjectId" placeholder="请选择科目" dictCode="cou_subject,subject_name,id"/>
            </a-form-model-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="试卷类型" prop="examinationPaperTtype">
                <j-dict-select-tag type="list"  v-model="queryParam.examinationPaperTtype" placeholder="选择试卷类型" dictCode="examination_paper_ttype"/>
              </a-form-model-item>
            </a-col>

<!--            <a-col :md="6" :sm="8">-->
<!--              <a-form-model-item label="启用状态">-->
<!--                <j-dict-select-tag type="list" v-model="queryParam.izOpen"-->
<!--                                   dictCode="iz_open" placeholder="选择是否启用状态" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!--上方Table区域 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增试卷</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('ach_examination_paper')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 快速生成试卷 信息填充弹窗 -->
      <a-button style='background-color: rgb(233, 175, 0);border: none;' @click="copyAchExamPaperVisible = true" type="primary" icon="copy">快速复制试卷</a-button>
      <div>
        <a-modal title="复制试卷" :visible="copyAchExamPaperVisible" @ok="copyAchExamPaper()" @cancel="copyAchExamPaperVisibleCancel" width='40%'>
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="试卷编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newExaminationPaperCode">
                <a-input v-model="copyNewAchExamInfo.newExaminationPaperCode" placeholder="请输入试卷编码"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="试卷名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newExaminationPaperName">
                <a-input v-model="copyNewAchExamInfo.newExaminationPaperName" placeholder="请输入试卷名称"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="所属考试" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newExaminationId">
                <j-dict-select-tag type="list" style="width: 70%" v-model="copyNewAchExamInfo.newExaminationId"
                                   dictCode="ach_examination,examination_name,id" placeholder="请选择所属考试" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="试卷类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newExaminationPaperTtype">
                <j-dict-select-tag type="list" style="width: 70%" v-model="copyNewAchExamInfo.newExaminationPaperTtype"
                                   dictCode="examination_paper_ttype" placeholder="请选择试卷类型" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-modal>
      </div>

    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys,  onChange: onSelectChange,type:type}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :customRow="clickThenCheck">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <!--==== 下方区域  试卷项目数据显示 ==== -->
    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="总分评分标准" key="1">
        <CallAchExamPaperStandard ref="CallAchExamPaperStandard"></CallAchExamPaperStandard>
      </a-tab-pane>
      <a-tab-pane tab="分项评分标准" key="2" forceRender>
        <CallAchExamPapProjectItemStandard ref="CallAchExamPapProjectItemStandard"></CallAchExamPapProjectItemStandard>
      </a-tab-pane>
      <a-tab-pane tab="题 型" key="3" forceRender>
        <AchExamPaperProjectItemList ref="AchExamPaperProjectItemList"></AchExamPaperProjectItemList>
      </a-tab-pane>
      <a-tab-pane tab="题目详情" key="4" forceRender>
        <AchExamPaperQuestionsItemList  ref="AchExamPaperQuestionsItemList"></AchExamPaperQuestionsItemList>
      </a-tab-pane>
    </a-tabs>
    <!--下方Table区域 操作按钮区域 -->

    <!--试卷新增 编辑Modal -->
    <ach-examination-paper-modal ref="modalForm" @ok="modalFormOk"></ach-examination-paper-modal>
    <!--试卷项目新增 编辑Modal -->
    <!--    <ach-exam-paper-project-item-modal ref="modalItemForm" @ok="modalItemFormOk"></ach-exam-paper-project-item-modal>-->
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import AchExaminationPaperModal from './modules/AchExaminationPaperModal'

import {getAchExamProjectItemByPaperId, getAchExamProjectItemStandard} from '@api/achievementCommonAPI'
import { getAction, deleteAction, postAction } from '@api/manage'
//引入试卷项目组件
import AchExamPaperProjectItemList from '@views/smart_school/achexampaperprojectitem/AchExamPaperProjectItemList'
import AchExamPaperProjectItemModal from '@views/smart_school/achexampaperprojectitem/modules/AchExamPaperProjectItemModal'

import CallAchExamPapProjectItemStandard from '@views/smart_school/achexaminationpaper/form/CallAchExamPapProjectItemStandard'
import CallAchExamPaperStandard from '@views/smart_school/achexaminationpaper/form/CallAchExamPaperStandard'

import AchExamPaperQuestionsItemList from '@views/smart_school/achexampaperquestionsitem/AchExamPaperQuestionsItemList'

export default {
  name: 'AchExaminationPaperList',
  mixins:[JeecgListMixin, mixinDevice],
  components: {
    AchExaminationPaperModal,
    AchExamPaperProjectItemModal,
    //页面下方项目标准、项目详情数据切换的组件调用
    AchExamPaperProjectItemList,
    CallAchExamPapProjectItemStandard,
    CallAchExamPaperStandard,//试卷总分标准
    AchExamPaperQuestionsItemList,//试卷题目
  },
  data () {
    return {
      description: 'ach_examination_paper管理页面',
      // 上方试卷表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title:'科目',
          align:"center",
          dataIndex: 'subjectName'
        },
        {
          title:'试卷编码',
          align:"center",
          dataIndex: 'examinationPaperCode'
        },
        {
          title:'试卷名称',
          align:"center",
          dataIndex: 'examinationPaperName'
        },
        {
          title:'所属考试',
          align:"center",
          dataIndex: 'examinationName'
        },
        {
          title:'试卷类型',
          align:"center",
          dataIndex: 'examinationPaperTtype_dictText'
        },
        {
          title:'试卷来源',
          align:"center",
          dataIndex: 'examinationPaperSource'
        },
        {
          title:'满分',
          align:"center",
          dataIndex: 'examinationPaperFullMarks'
        },
        {
          title:'及格分',
          align:"center",
          dataIndex: 'examinationPaperPassMarks'
        },
        {
          title:'考试时间',
          align:"center",
          dataIndex: 'examinationPaperTestTime',
          customRender:function (text) {
            return !text?"":(text.length>10?text.substr(0,10):text)
          }
        },
        {
          title:'考试时长(分钟)',
          align:"center",
          dataIndex: 'examinationPaperDuration'
        },
        {
          title:'是否启用',
          align:"center",
          dataIndex: 'izOpen_dictText'
        },
        {
          title:'所属部门',
          align:"center",
          dataIndex: 'sysOrgName'
        },
        {
          title:'是否删除',
          align:"center",
          dataIndex: 'delFlag_dictText'
        },
        // {
        //   title:'创建人',
        //   align:"center",
        //   dataIndex: 'createBy'
        // },
        {
          title:'创建时间',
          align:"center",
          dataIndex: 'createTime',
          customRender:function (text) {
            return !text?"":(text.length>10?text.substr(0,10):text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],

      url: {
        list: "/achexaminationpaper/achExaminationPaper/list",
        delete: "/achexaminationpaper/achExaminationPaper/delete",
        deleteBatch: "/achexaminationpaper/achExaminationPaper/deleteBatch",
        exportXlsUrl: "/achexaminationpaper/achExaminationPaper/exportXls",
        importExcelUrl: "achexaminationpaper/achExaminationPaper/importExcel",


        deleteItem: "/achexampaperprojectitem/achExamPaperProjectItem/delete",
        //复制试卷 快速复制试卷
        copyCreateAchExamPaper: "/achexaminationpaper/achExaminationPaper/copyCreateAchExamPaper",
      },
      dataSource: [],
      serchParams: {},
      // 分页参数
      type: "radio",
      dictOptions:{},
      superFieldList:[],
      achExaminationPaperVoList: [],
      /* 试卷的项目筛选参数 */
      filters2: {},
      /* 试卷的项目table加载状态 */
      loading2:false,
      achExaminationPaper: {
        id: null,
        subjectId: null,
        examinationPaperCode: null,
        examinationPaperName: null,
        examinationId: null,
        examinationPaperTtype: null,
        examinationPaperSource: null,
        examinationPaperFullMarks: null,
        examinationPaperPassMarks: null,
        examinationPaperTestTime: null,
        examinationPaperDuration: null,
        izOpen: null,
        sysOrgCode: null,
        tenantId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      },
      //复制试卷 填充新的试卷编码 试卷名称 所属考试 试卷类型
      copyNewAchExamInfo: {
        newExaminationPaperCode: null,
        newExaminationPaperName: null,
        newExaminationId: null,
        newExaminationPaperTtype: null,
      },
      copyAchExamPaperVisible: false,

      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '50', '100', '500', '1000'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },

    }
  },
  created() {
    this.getSuperFieldList();
    this.getAchExaminationPaperData();
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    clickThenCheck(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(","), [record]);
          }
        }
      };
    },
    initDictConfig(){
    },
    //下方table 显示切换
    changeTable: function (index) {
      this.number = index; //切换数据页面
    },
    getSuperFieldList(){
      let fieldList=[];
      fieldList.push({type:'string',value:'subjectId',text:'科目id',dictCode:''})
      fieldList.push({type:'string',value:'examinationPaperCode',text:'试卷编码',dictCode:''})
      fieldList.push({type:'string',value:'examinationPaperName',text:'试卷名称',dictCode:''})
      fieldList.push({type:'string',value:'examinationId',text:'所属考试id',dictCode:''})
      fieldList.push({type:'string',value:'examinationPaperTtype',text:'试卷类型',dictCode:''})
      fieldList.push({type:'string',value:'examinationPaperSource',text:'试卷来源',dictCode:''})
      fieldList.push({type:'double',value:'examinationPaperFullMarks',text:'满分',dictCode:''})
      fieldList.push({type:'double',value:'examinationPaperPassMarks',text:'及格分',dictCode:''})
      fieldList.push({type:'date',value:'examinationPaperTestTime',text:'考试时间'})
      fieldList.push({type:'int',value:'examinationPaperDuration',text:'考试时长(分钟)',dictCode:''})
      fieldList.push({type:'int',value:'izOpen',text:'是否启用(1是启用，0否停用)',dictCode:''})
      fieldList.push({type:'string',value:'sysOrgCode',text:'所属部门编码',dictCode:''})
      fieldList.push({type:'string',value:'tenantId',text:'学校id',dictCode:''})
      fieldList.push({type:'int',value:'delFlag',text:'0表示未删除,1表示删除',dictCode:''})
      fieldList.push({type:'string',value:'createBy',text:'创建人',dictCode:''})
      fieldList.push({type:'date',value:'createTime',text:'创建时间'})
      fieldList.push({type:'string',value:'updateBy',text:'修改人',dictCode:''})
      fieldList.push({type:'date',value:'updateTime',text:'修改时间'})
      this.superFieldList = fieldList
    },

    getAchExaminationPaperData(){
      // let achExaminationPaper = this.serchParams
      getAction(this.url.list,achExaminationPaper).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
        }
      });
    },

    //上方试卷table单选选择触发
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
      this.$refs.AchExamPaperProjectItemList.getOrderMain(this.selectionRows[0]); //selectionRows当前选择的试卷数据
      this.$refs.CallAchExamPapProjectItemStandard.getOrderMain(this.selectionRows[0]);
      this.$refs.CallAchExamPaperStandard.getOrderMain(this.selectionRows[0]);
      this.$refs.AchExamPaperQuestionsItemList.getOrderMain(this.selectionRows[0]); //selectionRows当前选择的试卷数据

      // let index = selectedRowKeys[0] //项目获取方法调用，当前选择试卷id参数
    },
    //复制试卷 快速复制试卷
    copyAchExamPaper(){
      if (this.selectionRows.length === 0){
        alert("请先选择一项你要复制的试卷模型")
      }else {
        //填充新的几项基本信息（试卷编码，试卷名称，所属考试，试卷类型）
        let achExaminationPaper = {
          id: this.selectionRows[0].id,//要复制试卷的id(旧试卷)
          examinationPaperCode: this.copyNewAchExamInfo.newExaminationPaperCode, //新填充数据，新试卷编码
          examinationPaperName: this.copyNewAchExamInfo.newExaminationPaperName, //新填充数据，新试卷名称
          examinationId: this.copyNewAchExamInfo.newExaminationId, //新填充数据，新试卷所属考试
          examinationPaperTtype: this.copyNewAchExamInfo.newExaminationPaperTtype, //新填充数据，新试卷试卷类型
        }
        postAction(this.url.copyCreateAchExamPaper,achExaminationPaper).then(res => {
          if (res.code === 200){
            this.$message.success("新的试卷生成完成")
            this.copyAchExamPaperVisible = false
          }else {
            this.$message.error("试卷生成出错，请检查数据是否完整")
          }
        });
      }
    },
    copyAchExamPaperVisibleCancel(){
      this.copyAchExamPaperVisible = false
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';

th.tableThColor {
  background-color: #0c8fcf !important;
}
</style>